// pages/shopdetails/shopdetails.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    info: {},
    statusd: 1
  },
  onLoad: function(options) {
    var _this = this
    _this.setData({
      id: options.id
    })
    _this.onShopInfo()
    setTimeout(() => {
      _this.setData({
        statusd: 0
      })
    }, 1500)
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {},
  //获取商品详情
  onShopInfo() {
    var _this = this
    com.link(app.link.item + _this.data.id + '.st', {}, function(res) {
      console.log(res)
      _this.setData({
        info: res.data.data
      })
    }, 'GET')
  },
  //更新数组库存内容
  valueNumber(e) {
    var _this = this,
      list = _this.data.info
    list.products.map((v, k) => {
      if (list.products[k].id == e.currentTarget.id) {
        console.log(list.products[k].id)
        list.products[k].number = e.detail.value
      }
    })
    _this.setData({
      info: list
    })
    console.log(e)
  },
  //更新库存
  onCartNumber(e) {
    var _this = this,
      d = e.currentTarget.id
    if (d == 1) {
      wx.navigateBack({
        delta: 1
      })
    } else {
      com.link(app.link.product + _this.data.id + '.st', {
        products: _this.data.info.products,
        sale: _this.data.info.selected ? 1 : 0
      }, function(res) {
        console.log(res);
        if (res.data.state) {
          wx.showToast({
            title: res.data.message,
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 1500)
        }
      })
    }
  },
  //保存上下架
  selected(e) {
    var _this = this,
      l = _this.data.info,
      sale = e.detail.value
    l.selected = sale == 1 ? true : false
    _this.setData({
      info: l
    })
  }
})